package com.lbn.jfqsc.app.account.resume;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.EditText;

import com.google.gson.Gson;
import com.lbn.baseframe.base.BaseActivity;
import com.lbn.baseframe.ui.NAppbar;
import com.lbn.baseframe.utils.SPUtils;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResumeActivity extends BaseActivity<ResumePresenter> implements IResume {

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.skill)
    EditText skill;
    @BindView(R.id.introduce)
    EditText introduce;
    @BindView(R.id.experience)
    EditText experience;
    @BindView(R.id.checkbox)
    CheckBox checkbox;


    private final String KEY = App.getUser().getPhone()+"resume";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resume);
        ButterKnife.bind(this);
        setAppbar(new NAppbar(this));
        appbar.setTitle("我的简历");
        appbar.openBack();
    }


    public String getName() {
        return name.getText().toString();
    }

    public boolean getCheckbox() {
        return checkbox.isChecked();
    }

    public String getSkill() {
        return skill.getText().toString();
    }

    public String getExperience() {
        return experience.getText().toString();
    }

    public String getIntroduce() {
        return introduce.getText().toString();
    }

    @Override
    public ResumePresenter createPresenter() {
        return new ResumePresenter();
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, ResumeActivity.class));
    }

    @OnClick(R.id.confirm)
    public void onClick() {
        mPresenter.save(this);
    }


    @Override
    public void onPresenterCreated() {
        super.onPresenterCreated();
        ResumeBean resumeBean = null;
        if(SPUtils.contains(this,KEY)){
            String json = (String) SPUtils.get(this,KEY,"");
            resumeBean = new Gson().fromJson(json,ResumeBean.class);
        }
        if(resumeBean!=null){
            name.setText(resumeBean.getName());
            experience.setText(resumeBean.getExperience());
            introduce.setText(resumeBean.getIntroduce());
            skill.setText(resumeBean.getSkills());
            checkbox.setChecked(resumeBean.isOpen());
        }
    }

    @Override
    public void onSucceed() {
        ResumeBean resume = new ResumeBean();
        resume.setName(getName());
        resume.setExperience(getExperience());
        resume.setIntroduce(getIntroduce());
        resume.setSkills(getSkill());
        resume.setOpen(getCheckbox());
        SPUtils.put(this,KEY,new Gson().toJson(resume));
    }
}
